package jp.co.recruit_lifestyle.sample.slice;

import jp.co.recruit_lifestyle.ohos.widget.WaveSwipeRefreshLayout;
import jp.co.recruit_lifestyle.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Slider;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.Random;

public class MainAbilitySlice extends AbilitySlice implements WaveSwipeRefreshLayout.OnRefreshListener {

    private EventHandler mHandler = new EventHandler(EventRunner.current());

    private ListContainer mListContainer;
    private WaveSwipeRefreshLayout mWaveSwipeRefreshLayout;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        initView();
        setSampleData();
    }


    private void initView() {
        mWaveSwipeRefreshLayout = (WaveSwipeRefreshLayout) findComponentById(ResourceTable.Id_main_swipe);

        mWaveSwipeRefreshLayout.setColorSchemeColors(Color.WHITE.getValue(), Color.WHITE.getValue());
        mWaveSwipeRefreshLayout.setOnRefreshListener(this);
        mWaveSwipeRefreshLayout.setWaveRGBColor(255, 156, 156);

        mListContainer = (ListContainer) findComponentById(ResourceTable.Id_main_list);

        findComponentById(ResourceTable.Id_button_of_wave_color).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Random random = new Random();
                int r = random.nextInt(256);
                int g = random.nextInt(256);
                int b = random.nextInt(256);

                mWaveSwipeRefreshLayout.setWaveColor(new RgbColor(r, g, b).asArgbInt()); // Random assign
            }
        });
        Slider slider = (Slider) findComponentById(ResourceTable.Id_seekbar_of_drop_height);

        ShapeElement rounderShapeElement = new ShapeElement();
        rounderShapeElement.setRgbColor(new RgbColor(0, 150, 136));
        rounderShapeElement.setShaderType(ShapeElement.OVAL);
        rounderShapeElement.setCornerRadius(100);
        slider.setThumbElement(rounderShapeElement);

        slider.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                return true;
            }
        });
        slider.setValueChangedListener(new Slider.ValueChangedListener() {
            @Override
            public void onProgressUpdated(Slider slider, int i, boolean b) {

            }

            @Override
            public void onTouchStart(Slider slider) {

            }

            @Override
            public void onTouchEnd(Slider slider) {
                float scale = (slider.getProgress() / 100f);

                if (minHeight > 0 && maxHeight > 0) {
                    float factor = maxHeight - minHeight;
                    mWaveSwipeRefreshLayout.setMaxDropHeight((int) (minHeight + factor * scale));
                } else {
                    mWaveSwipeRefreshLayout.setMaxDropHeight((int) (mWaveSwipeRefreshLayout.getHeight() * scale));
                }
            }
        });
        slider.setBindStateChangedListener(new Component.BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                getUITaskDispatcher().asyncDispatch(new Runnable() {
                    @Override
                    public void run() {
                        int height = mWaveSwipeRefreshLayout.getEstimatedHeight();
                        int width = mWaveSwipeRefreshLayout.getEstimatedWidth();
                        minHeight = 500 * (width / 1440.f);
                        maxHeight = height - width / 14.4f;
                    }
                });
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    float minHeight;
    float maxHeight;

    private void setSampleData() {
        ArrayList<String> sampleArrayStr = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            sampleArrayStr.add("");
        }
        ArrayAdapter adapter = new ArrayAdapter(getContext(), sampleArrayStr);
        mListContainer.setItemProvider(adapter);
    }

    private void refresh() {
        mHandler.postTask(new Runnable() {
            @Override
            public void run() {
                // 更新が終了したらインジケータ非表示
                mWaveSwipeRefreshLayout.setRefreshing(false);
            }
        }, 3000);
    }

    @Override
    public void onActive() {
        //mWaveSwipeRefreshLayout.setRefreshing(true);
        refresh();
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onRefresh() {
        refresh();
    }
}
